package dao.purchasing;

import dao.BaseDaoHibernate;
import model.purchasing.OrderMst;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;


@Repository
public class OrderDao extends BaseDaoHibernate<OrderMst>{
    
    private static final Logger log = Logger.getLogger(OrderDao.class);
    
    public boolean saveData(OrderMst domain) {
        Session session = sessionFactory.openSession();
        boolean saved = false;
        try {
            session.beginTransaction();
                                
            domain.setOrderDetails(domain.getOrderDetails());
            sessionFactory.getCurrentSession().saveOrUpdate(domain);
            
            long orderId = domain.getId();
            for (int i=0; i<domain.getOrderDetails().size(); i++) {
                domain.getOrderDetails().get(i).getId().setId(orderId);
            }

            session.getTransaction().commit();
            saved=true;
        } catch (RuntimeException e) {
            session.getTransaction().rollback(); 
            log.error("gagal", e);
            java.util.logging.Logger.getLogger(OrderDao.class.getName()).log(java.util.logging.Level.SEVERE, null, e);
        } finally {
            if (session != null) { session.close();}
            return saved;
        }
    }

}
